﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Lidgren.Network;

namespace Erebos.CharackterScreenFiles
{
    class CharackterSelectScreen : Screen
    {
        private List<Charackter> _chars;
        private int _selectedChar = 0;

        public CharackterSelectScreen(List<Charackter> chars)
        {
            if (chars == null)
                throw new ArgumentNullException("Argument 'chars' mustn't null or nothing.");
            _chars = chars;
            if (_chars.Count != 0)
            {
                _selectedChar = 1;
            }

            AddEvent(new Rectangle(41, ScreenSizeHelper.SizeHeight - MainGame.Textures["Box/ButtomSide"].Height - MainGame.Textures["PlayButton"].Height - 14, MainGame.Textures["PlayButton"].Width, MainGame.Textures["PlayButton"].Height), PlayButton);
        }

        private void PlayButton(object sender, EventArgs e)
        {
            SendCollection<object, VariableType> collection = new SendCollection<object,VariableType>();
            collection.Add((byte)0x03, VariableType.Byte);
            collection.Add(_chars[_selectedChar - 1].Name, VariableType.String);
            ClientHandler.Send(collection, NetDeliveryMethod.Unreliable);
            ClientHandler.AddHandle((byte)0x03, OnIncoming_0x03);
        }

        private void OnIncoming_0x03(NetIncomingMessage message)
        {
            MainGame.SetScreen(new GameScreenFiles.GameScreen(_chars[_selectedChar - 1]));
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            MultiTexture2D box = new MultiTexture2D(MainGame.Textures["Box/LeftTopCorner"], MainGame.Textures["Box/TopSide"], MainGame.Textures["Box/RightTopCorner"], MainGame.Textures["Box/RightSide"], MainGame.Textures["Box/RightButtomCorner"], MainGame.Textures["Box/ButtomSide"], MainGame.Textures["Box/LeftButtomCorner"], MainGame.Textures["Box/LeftSide"]);
            box.EnableFill(MainGame.Textures["Box/Fill"]);
            box.Draw(spriteBatch, new Rectangle(15, 15, 250, ScreenSizeHelper.SizeHeight - 30), Color.White);

            if (_selectedChar != 0)
            {
                Vector2 txtSize = MainGame.Fonts["Arial10"].MeasureString("Charackterinformation");
                Vector2 txtPos = new Vector2(125 - txtSize.X / 2 + 15, 47);
                spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Charackterinformation", txtPos, Color.White);
                spriteBatch.Draw(MainGame.Textures["Strich250"], new Vector2(37, txtPos.Y + 20), Color.White);
                spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Name: " + _chars[_selectedChar - 1].Name, new Vector2(42, txtPos.Y + 28), Color.White);
                if(_chars[_selectedChar - 1].Klasse == CharackterKlasse.Erebos)
                    spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Level: EREBOS", new Vector2(42, txtPos.Y + 44), Color.White);
                else
                    spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Level: " + _chars[_selectedChar - 1].Level, new Vector2(42, txtPos.Y + 44), Color.White);
                spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Klasse: " + _chars[_selectedChar - 1].Klasse.ToString(), new Vector2(42, txtPos.Y + 60), Color.White);
                spriteBatch.DrawString(MainGame.Fonts["Arial10"], "Spezialisierung: " + _chars[_selectedChar - 1].Job.ToString(), new Vector2(42, txtPos.Y + 76), Color.White);
                //Check for spezial rights
                if (_chars[_selectedChar - 1].Rights.ToString() == "20000") //Moderator
                {
                    Rectangle pos = new Rectangle(125 - MainGame.Textures["Rang/Mod"].Width / 4 + 15, (int)txtPos.Y + 90, MainGame.Textures["Rang/Mod"].Width / 2, MainGame.Textures["Rang/Mod"].Height / 2);
                    spriteBatch.Draw(MainGame.Textures["Rang/Mod"], pos, Color.White);
                }
                else if (_chars[_selectedChar - 1].Rights.ToString() == "22001") //Bote
                {
                    Rectangle pos = new Rectangle(125 - MainGame.Textures["Rang/Bote"].Width / 4 + 15, (int)txtPos.Y + 90, MainGame.Textures["Rang/Bote"].Width / 2, MainGame.Textures["Rang/Bote"].Height / 2);
                    spriteBatch.Draw(MainGame.Textures["Rang/Bote"], pos, Color.White);

                }
                else if (_chars[_selectedChar - 1].Rights.ToString() == "22221") //Super-Bote
                {
                    Rectangle pos = new Rectangle(125 - MainGame.Textures["Rang/SuperBote"].Width / 4 + 15, (int)txtPos.Y + 90, MainGame.Textures["Rang/SuperBote"].Width / 2, MainGame.Textures["Rang/SuperBote"].Height / 2);
                    spriteBatch.Draw(MainGame.Textures["Rang/SuperBote"], pos, Color.White);
                }
                else if (_chars[_selectedChar - 1].Rights.ToString() == "22222") //Admin
                {
                    Rectangle pos = new Rectangle(125 - MainGame.Textures["Rang/Admin"].Width / 4 + 15, (int)txtPos.Y + 90, MainGame.Textures["Rang/Admin"].Width / 2, MainGame.Textures["Rang/Admin"].Height / 2);
                    spriteBatch.Draw(MainGame.Textures["Rang/Admin"], pos, Color.White);
                }
                spriteBatch.Draw(MainGame.Textures["PlayButton"], new Vector2(41, ScreenSizeHelper.SizeHeight - MainGame.Textures["Box/ButtomSide"].Height - MainGame.Textures["PlayButton"].Height - 14), Color.White);
            }

            if (_chars.Count == 1)
            {
                spriteBatch.Draw(MainGame.Textures["TmpMensch"], new Vector2(310, 42), Color.White);
            }

            base.Draw(spriteBatch, gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
    }
}
